/**
 * JavaScript Document
 * @author sijian zhang
 */

$sj.util = {
    addEvent:function (eventType, fn ,obj ){
		obj = document.getElementById(obj) || obj || this;
		if (obj.addEventListener){
			obj.addEventListener(eventType, fn , false );
		}
		else if (obj.attachEvent){
			obj.attachEvent("on" + eventType, fn);
		}
		return obj
	},
	removeEvent : function(eventType, fn ,obj) {
		obj = document.getElementById(obj) || obj || this;
		if (obj.removeEventListener){
			obj.removeEventListener(eventType , fn, false);
		} else if (obj.detachEvent){
			obj.detachEvent('on' + eventType , fn);
		}
		return obj
	},
	addClass : function(cla,obj){
		ele = document.getElementById(obj) || obj || this;
		if (ele.className.indexOf(cla) == -1){
			ele.className = (ele.className + ' ' + cla).split(new RegExp(/\s/));
		}
		return ele;
	},
	removeClass : function(cla,obj){
		ele = document.getElementById(obj) || obj || this;
		ele.className = ele.className.toString().replace(new RegExp(cla),"");
		return ele;
	},
	hasClass : function(cla,obj){
		return obj.className.toString().indexOf(cla)
	},
	hidden : function(obj){
		obj = document.getElementById(obj) || obj || this;
		obj.style.visibility = "hidden";
		obj.style.position = "absolute";
	},
	show :function(obj){
		obj = document.getElementById(obj) || obj || this;
		obj.style.visibility = "";
		obj.style.position = "";
	},
	setOpacity : function(obj , opacity) {
		obj = document.getElementById(obj) || obj || this;
		if (window.ActiveXObject) {
			obj.style.filter = (opacity == 100) ? "" : "gray() alpha(opacity=" + opacity + ")";
		} else {
			obj.style.opacity = (opacity == 100) ? "" : "0." + opacity.toString();
		}
		return obj;
	},
	
	getObjHeight: function(el) {
		el = $obj(el) || el|| this || $sj.getDoc()
		return Math.max(el.scrollHeight, el.clientHeight);
	},
	getObjWidth: function(el) {
		el = $obj(el) || el || this || $sj.getDoc()
		return Math.max(el.scrollWidth, el.clientWidth);
	},
	getMousePos : function() {
		ev = arguments[0] || window.event;
		el = $sj.getDoc();
		return {"x" : ev.clientX + el.scrollLeft - el.clientLeft ,"y" : ev.clientY + el.scrollTop - el.clientTop}||{ "x" : ev.pageX, "y" : ev.pageY};
	},
	getObjPos : function(el) {
		el = $obj(el) || el || this || $sj.getDoc()
		el.x = 0;
		el.y = 0;
		if (el.getBoundingClientRect) {
			box = el.getBoundingClientRect();
			el = $sj.getDoc();
			el.x = box.left + el.scrollLeft - el.clientLeft;
			el.y = box.top + el.scrollTop - el.clientTop;
			box = null
		} else {
			el.x = el.offsetLeft;
			el.y = el.offsetTop;
			var parent = el.offsetParent;
			while (parent) {
				el.x += parent.offsetLeft;
				el.y += parent.offsetTop;
				parent = parent.offsetParent;
			}
			parent = null
		}
		return {"x" : el.x , "y" : el.y};
	}
	//Extend end
}